package com.xiaoyao.myframeapp.utils.Dialog;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.widget.EditText;

import com.xiaoyao.myframeapp.R;


/**
 * DialogUtilwowo
 */
public class DialogUtil {

    public static ProgressDialog getDialog(Context context) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setMessage(context.getResources().getString(R.string.loading));
        return dialog;
    }

    public interface AlertDialogListener {
        void positiveButtonClick(DialogInterface dialog, int which);

        void negativeButtonClick(DialogInterface dialog, int which);
    }

    public interface AlertDialogListenerChecked {
        void positiveButtonClick(DialogInterface dialog, int which, boolean isChecked);

        void negativeButtonClick(DialogInterface dialog, int which, boolean isChecked);
    }

    public interface AlertDialogListenerString {
        void positiveButtonClick(DialogInterface dialog, String string);

        void negativeButtonClick(DialogInterface dialog, String string);
    }

    /**
     * 提示
     *
     * @param context
     * @param title
     * @param listener
     */
    public static void getAlertDialog(Context context, String title, final AlertDialogListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                context);
        builder.setMessage(title);
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                                        int which) {
                        if (listener != null)
                            listener.positiveButtonClick(dialog, which);

                    }
                });
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                                        int which) {
                        if (listener != null)
                            listener.negativeButtonClick(dialog, which);
                    }
                });
        builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                listener.negativeButtonClick(dialog, keyCode);
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    dialog.dismiss();
                }
                return false;
            }
        });

        AlertDialog dialog = builder.create();
        //TODO 注意这里是 false
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    /**
     * 提示 是否
     *
     * @param context
     * @param title
     * @param listener
     */
    public static void getAlertDialog_Pay(Context context, String title, final AlertDialogListener listener) {
        new AlertDialog.Builder(context)
                .setMessage(title)
                .setPositiveButton("是",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                if (listener != null)
                                    listener.positiveButtonClick(dialog, which);

                            }
                        })
                .setNegativeButton("否",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                if (listener != null)
                                    listener.negativeButtonClick(dialog, which);
                            }
                        })
                .setOnKeyListener(
                        new DialogInterface.OnKeyListener() {
                            @Override
                            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                                if (listener != null)
                                    listener.negativeButtonClick(dialog, 0);
                                return false;
                            }
                        }

                )
//        AlertDialog dialog = builder.create();
//        dialog.setCancelable(true);
//        dialog.show();
                .show();
    }

    /**
     * 提示 是否 + 返回键无效
     *
     * @param context
     * @param title
     * @param listener
     */
    public static void getAlertDialog_Pay_NoBack(Context context, String title, final AlertDialogListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                context);
        builder.setMessage(title)
                .setPositiveButton("是",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                if (listener != null)
                                    listener.positiveButtonClick(dialog, which);
                            }
                        })
                .setNegativeButton("否",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                if (listener != null)
                                    listener.negativeButtonClick(dialog, which);
                            }
                        })
                .setOnKeyListener(
                        new DialogInterface.OnKeyListener() {
                            @Override
                            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

//                                listener.negativeButtonClick(dialog, keyCode);
                                if (keyCode == KeyEvent.KEYCODE_BACK
                                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                                    dialog.dismiss();
                                }
                                return false;
                            }
                        }
                );
        AlertDialog dialog = builder.create();
        //TODO 注意这里是 false
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    public static void showMessageDialog(Context context, String title) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                context);
        builder.setMessage(title);
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                                        int which) {
                    }
                });
        AlertDialog dialog = builder.create();
        dialog.setCancelable(true);
        dialog.show();
    }

    public static void showMessageDialog_OnClick(Context context, String title, final AlertDialogListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                context);
        builder.setMessage(title);
        builder.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                                        int which) {
                        if (listener != null)
                            listener.positiveButtonClick(dialog, which);
                    }
                });
        builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//                listener.negativeButtonClick(dialog, keyCode);
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == KeyEvent.ACTION_DOWN) {
                    return true; //TODO 返回键无效
//                    return false;//TODO 回键
//                    dialog.dismiss();
                }
                return false;
            }
        });
        final AlertDialog dialog = builder.create();
//        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
    }


    /**
     * 多选框
     */
    public static void DialogCheck_Box(Context context, String[] items, boolean[] flags, String s, final AlertDialogListenerChecked listener) {

        new AlertDialog.Builder(context).setTitle(s).setMultiChoiceItems(
                items, flags, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if (listener != null) {
                            listener.positiveButtonClick(dialog, which, isChecked);
                        } else {
                            dialog.dismiss();
                        }
                    }
                })
                .setPositiveButton("确定", null)/*
                .setNegativeButton("取消", null)*/.show();


    }

    /**
     * 单选框 无取消
     */
    public static void DialogRadio_Box_NoCancel(final Context context, String[] wowo1type, String s, final AlertDialogListener listener) {
        new AlertDialog.Builder(context).setTitle(s)
                .setSingleChoiceItems(
                        wowo1type, -1,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                if (listener != null) {
                                    listener.positiveButtonClick(dialog, which);
                                }
                                dialog.dismiss();
                            }
                        })
                .setOnKeyListener(
                        new DialogInterface.OnKeyListener() {
                            @Override
                            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                                listener.negativeButtonClick(dialog, 0);
                                return false;
                            }
                        }

                )
                .show();

    }

    /**
     * 单选框
     */
    public static void DialogRadio_Box(Context context, String[] wowo1type, String s, final AlertDialogListener listener) {
        new AlertDialog.Builder(context).setTitle(s).setSingleChoiceItems(
                wowo1type, 0,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (listener != null) {
                            listener.positiveButtonClick(dialog, which);
                        }
                        dialog.dismiss();

                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.negativeButtonClick(dialog, which);
                    }
                })
                .show();

    }

    /**
     * 输入框
     */
    public static void DialogRadio_Input_Box(final Context context, String string, String data, final AlertDialogListenerString listener) {
        final EditText editText = new EditText(context);
//        editText.setBackgroundResource(R.drawable.ic_com_sina_weibo_sdk_button_grey_focused);
        editText.setText(data);
        final AlertDialog.Builder builder = new AlertDialog.Builder(context).setTitle(string)
                .setView(editText)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String s = editText.getText().toString();
                        listener.positiveButtonClick(dialog, s);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.negativeButtonClick(dialog, "");
                    }
                })
                .setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        listener.negativeButtonClick(dialog, "");
                        if (keyCode == KeyEvent.KEYCODE_BACK
                                && event.getAction() == KeyEvent.ACTION_DOWN) {
                            dialog.dismiss();
                        }
                        return false;
                    }
                });
        final AlertDialog dialog = builder.create();

        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

    }

    /**
     * 拨打电话 定制
     *
     * @param context
     * @param title
     * @param listener
     */
    public static void Customization(Context context, String title, String l, String r
            , final AlertDialogListener listener) {
        new AlertDialog.Builder(context)
                .setMessage(title)
                .setNegativeButton(l,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                if (listener != null)
                                    listener.negativeButtonClick(dialog, which);
                            }
                        })
                .setPositiveButton(r,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                if (listener != null)
                                    listener.positiveButtonClick(dialog, which);

                            }
                        })
                .setOnKeyListener(
                        new DialogInterface.OnKeyListener() {
                            @Override
                            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                                if (listener != null)
                                    listener.negativeButtonClick(dialog, 0);
                                return false;
                            }
                        }

                )
//        AlertDialog dialog = builder.create();
//        dialog.setCancelable(true);
//        dialog.show();
                .show();
    }


    /**
     *
     */
    public static void Dialog__Customization(final Context context, String string, String hint, final AlertDialogListener listener) {
//        final EditText editText = new EditText(context);
//        editText.setBackgroundResource(R.drawable.ic_com_sina_weibo_sdk_button_grey_focused);
//        editText.setHint(hint);
//        editText.setGravity(Gravity.CENTER);
//        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)});//TODO 手动设置密码长度
//        editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        final AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(string)
//                .setView(editText)
                .setMessage(hint)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        String s = editText.getText().toString();
                        listener.positiveButtonClick(dialog, which);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.negativeButtonClick(dialog, which);
                    }
                })
                .setOnKeyListener(new DialogInterface.OnKeyListener() {
                    @Override
                    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                        listener.negativeButtonClick(dialog, 0);
                        if (keyCode == KeyEvent.KEYCODE_BACK
                                && event.getAction() == KeyEvent.ACTION_DOWN) {
                            dialog.dismiss();
                        }
                        return false;
                    }
                });
        final AlertDialog dialog = builder.create();

        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

    }
}
